package com.ruoyi.project.jiaqin.newscomment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.news.mapper.NewsMapper;
import com.ruoyi.project.jiaqin.newscomment.domain.NewsComment;
import com.ruoyi.project.jiaqin.newscomment.mapper.NewsCommentMapper;
import com.ruoyi.project.jiaqin.newscomment.service.INewsCommentService;
import com.ruoyi.project.jiaqin.newscommentlikers.domain.NewsCommentLikers;
import com.ruoyi.project.jiaqin.newscommentlikers.service.INewsCommentLikersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 新闻评论Service业务层处理
 *
 * @author admin
 * @date 2020-12-15
 */
@Service
public class NewsCommentServiceImpl implements INewsCommentService
{
    @Autowired
    private NewsCommentMapper newsCommentMapper;
    @Autowired
    private NewsMapper newsMapper;

    /**
     * 查询新闻评论
     *
     * @param id 新闻评论ID
     * @return 新闻评论
     */
    @Override
    public NewsComment selectNewsCommentById(String id)
    {
        return newsCommentMapper.selectNewsCommentById(id);
    }

    @Override
    public NewsComment selectNewsCommentByIdA(String id) {
        return newsCommentMapper.selectNewsCommentByIdA(id);
    }

    /**
     * 查询新闻评论列表
     *
     * @param newsComment 新闻评论
     * @return 新闻评论
     */
    @Override
    public List<NewsComment> selectNewsCommentList(NewsComment newsComment)
    {
        return newsCommentMapper.selectNewsCommentList(newsComment);
    }

    @Autowired
    private INewsCommentLikersService newsCommentLikersService;
    @Autowired
    private ICompanyService companyService;
    @Override
    public PageInfo<NewsComment> getNewsCommentByNewsId(Integer pageNum,Integer pageSize,String newsId, String memberId) {
        PageHelper.startPage(pageNum,pageSize);
        List<NewsComment> list = newsCommentMapper.selectNewsCommentListA(newsId);
        for (NewsComment NC : list) {
            NewsCommentLikers newsCommentLikers = new NewsCommentLikers();
            newsCommentLikers.setMemberId(memberId);
            newsCommentLikers.setToMemberId(NC.getUserId());
            newsCommentLikers.setNewsCommentId(NC.getId());
            List<NewsCommentLikers> newsCommentLikersList = newsCommentLikersService.selectNewsCommentLikersList(newsCommentLikers);
            // 是否点赞 0-否 1-是
            if (newsCommentLikersList.size() > 0) {
                NC.setIsLikers(1);
            } else {
                NC.setIsLikers(0);
            }
            // 点赞数
            int likersNum = newsCommentLikersService.countNewsCommentLikersByNewsCommentId(NC.getId());
            NC.setLikersNum(likersNum);
            // 是否是企业
            Company company = companyService.selectCompanyByMemberId(NC.getUserId());
            if (company != null) {
                NC.setIsCompany(1);
            } else {
                NC.setIsCompany(0);
            }
            NC.setMemberCompanyId(NC.getUserId());
        }
        PageInfo<NewsComment> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public int commentNews(NewsComment newsComment) {
        insertNewsComment(newsComment);
        return newsMapper.updateCommentNum(1L,newsComment.getNewsId());
    }

    /**
     * 新增新闻评论
     *
     * @param newsComment 新闻评论
     * @return 结果
     */
    @Override
    public int insertNewsComment(NewsComment newsComment)
    {
        newsComment.setId(UUID.randomUUID().toString().replace("-",""));
        return newsCommentMapper.insertNewsComment(newsComment);
    }

    /**
     * 修改新闻评论
     *
     * @param newsComment 新闻评论
     * @return 结果
     */
    @Override
    public int updateNewsComment(NewsComment newsComment)
    {
        return newsCommentMapper.updateNewsComment(newsComment);
    }

    /**
     * 删除新闻评论对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNewsCommentByIds(String ids)
    {
        return newsCommentMapper.deleteNewsCommentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除新闻评论信息
     *
     * @param id 新闻评论ID
     * @return 结果
     */
    @Override
    public int deleteNewsCommentById(String id)
    {
        return newsCommentMapper.deleteNewsCommentById(id);
    }
}
